* ----------------------------------------------------------------------------- *
* Natural Resource Rents, Local Taxes, and Government Performance: Evidence from
* Colombia
* By: Luis R. Martinez
* Name: 20-estimation.do
* Description: This do file produces all the tables and figures in the body of the paper.
* ----------------------------------------------------------------------------- *

clear all 
cls 

* Explanatory vars for reduced-form regs
global rf_log post_update0610any lroy
global rf_level post_update0610any roy_ipoil
global rf_port post_update0610any roilpcravg_iprcopr
global rf_period post_sum lr_oilpcr00_prcopr

* Explanatory vars for iv regs
global iv_log (ly_tax_pred_pcr ly_k_rega_pcr = post_update0610any lroy )
global iv_port (y_tax_pred_pcr y_k_rega_pcr = post_update0610any roilpcravg_iprcopr)
global iv_level (y_tax_pred_pcr y_k_rega_pcr = post_update0610any roy_ipoil)
global iv_tax_level (y_tax_pred_pcr = post_update0610any)
global iv_roy_level (y_k_rega_pcr = roy_ipoil)
global iv_period (ly_tax_pred_pcr ly_k_rega_pcr = post_sum lr_oilpcr00_prcopr)
global ols_level y_tax_pred_pcr y_k_rega_pcr
global ols_period ly_tax_pred_pcr ly_k_rega_pcr 

* Sample restrictions: (i) 2005-2011, (ii) only IGAC, (iii) valid: not missing key vars 
global if if year>=2005&year<=2011&valid==1
global if_period if igac==1&corregimiento==0

* Fixed effects
global fe absorb(codmpio i.year#i.coddepto)
global fe_mm absorb(i.codmpio#i.term i.year#i.coddepto) 
global fe_initial absorb(codmpio i.year#c.\`var' i.year#i.coddepto)
global fe_period absorb(codmpio i.coddepto#i.period)
global fe_category absorb(codmpio i.year#i.coddepto#i.category)

* Two-way clustering of standard errors
global cluster cluster(codmpio dpt_year) 
global cluster_muni cluster(codmpio) 
global cluster_period cluster(codmpio dpt_period) 

* First-stage F-stats
global F_tax local F_tax: di %4.2fc e(first)[8,1]
global F_roy local F_roy: di %4.2fc e(first)[8,2]
global F_roy1 local F_roy: di %4.2fc e(widstat)

* p-values of difference in IV estimates
global test_log	test ly_tax_pred_pcr = ly_k_rega_pcr
global test_level test y_tax_pred_pcr = y_k_rega_pcr
global p_val local p_val: di %4.3fc r(p)

* Additional stats for IV regs
global add_iv addtext(F-stat tax, \`F_tax', F-stat royalties,  \`F_roy', p-value H0:tax=royalties,"\`p_val'")
global add_iv_tax addtext(F-stat, \`F_tax')
global add_iv_roy addtext(F-stat, \`F_roy')
global add_ols addtext(p-value H0:tax=royalties,"\`p_val'")

*-------------------------------------------------------------------------------*
**# A) TABLES 
*-------------------------------------------------------------------------------*


* Table 1: Revenue and Spending ------------------------------------------------

use "${dpwork}\ESTIMATIONS.dta", clear

** Panel A:
local out outreg2 using "${dpmain}\01-table1_a", dec(3) tex(frag) nonot nocons

*** Column 1:
reghdfe y_tax_pred_pcr $rf_level ${if}, $fe $cluster  
	sum y_tax_pred_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(prop_tax) replace 	

*** Column 2:
reghdfe ly_tax_pred_pcr $rf_log ${if}, $fe $cluster   
	sum y_tax_pred_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(prop_tax) 	

*** Column 3:	
reghdfe y_k_rega_pcr $rf_level ${if}, $fe $cluster 
	sum y_k_rega_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(royalties)

*** Column 4:	
reghdfe ly_k_rega_pcr $rf_log ${if}, $fe $cluster 
	sum y_k_rega_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(royalties)

*** Column 5:	
reghdfe ly_total_pcr $rf_log ${if}, $fe $cluster 
	sum y_total_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(total_revenue)

*** Column 6:	
reghdfe lg_total_pcr $rf_log ${if}, $fe $cluster 
	sum g_total_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(total_spending)

*** Column 7:	
reghdfe li_pcr $rf_log ${if}, $fe $cluster 
	sum i_pcr if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(investment) 
	
	
** Panel B:	
local out outreg2 using "${dpmain}\01-table1_b", dec(3)  tex(frag) nonot nocons nor2

*** Column 5:
ivreghdfe ly_total_pcr $iv_log ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(tax_revenue) drop(ly_tax_pred_pcr ly_k_rega_pcr) noobs replace 
	`out' ctitle(tax_revenue) drop(ly_tax_pred_pcr ly_k_rega_pcr) noobs
	`out' ctitle(royalties) drop(ly_tax_pred_pcr ly_k_rega_pcr) noobs
	`out' ctitle(royalties) drop(ly_tax_pred_pcr ly_k_rega_pcr) noobs
	`out' ctitle(total_revenue) $add_iv 

*** Column 6:	
ivreghdfe lg_total_pcr $iv_log ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(total_spending) $add_iv

*** Column 7:	
ivreghdfe li_pcr $iv_log ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(investment) $add_iv 

* Table 2: Public Goods --------------------------------------------------------

use "${dpwork}\ESTIMATIONS.dta", clear

** Panel A:
local out outreg2 using "${dpmain}\02-table2_a", dec(3)  tex(frag) nonot nocons

*** Column 1: 
reghdfe lcnbas $rf_level ${if}, $fe $cluster   
sum cnbas if e(sample)
`out' adds(DV mean,r(mean)) ctitle(cnbas) replace	

*** Column 2: 
reghdfe lcobrs $rf_level ${if}, $fe $cluster 
	sum cobrs if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(cobrs)

*** Column 3:
reghdfe lirca $rf_level ${if}, $fe $cluster 
	sum irca if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(irca)

*** Column 4:	
reghdfe Dcnbas100 $rf_level ${if}, $fe $cluster   
	sum Dcnbas100 if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(Dcnbas) 	

*** Column 5:	
reghdfe Dcobrs100 $rf_level ${if}, $fe $cluster 
	sum Dcobrs100 if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(Dcobrs) 

*** Column 6:	
reghdfe Dirca05 $rf_level ${if}, $fe $cluster 
	sum Dirca05 if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(Dirca) 

	
** Panel B:	
local out outreg2 using "${dpmain}\02-table2_b", dec(3) tex(frag) nonot nocons nor2

*** Column 1:
ivreghdfe lcnbas $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(cnbas) $add_iv replace

*** Column 2:
ivreghdfe lcobrs $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(cobrs) $add_iv

*** Column 3:
ivreghdfe lirca $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(lirca) $add_iv

*** Column 4:
ivreghdfe Dcnbas100 $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(Dcnbas) $add_iv

*** Column 5:
ivreghdfe Dcobrs100 $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(Dcobrs) $add_iv

*** Column 6:
ivreghdfe Dirca05 $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(Dirca) $add_iv 

* Table 3: Infant Health -------------------------------------------------------

use "${dpwork}\ESTIMATIONS.dta", clear

foreach x of varlist consultas0_pc low_weight_pc {
	replace `x'=`x'*100
}

** Panel A:
local out outreg2 using "${dpmain}\03-table3_a", dec(3)  tex(frag) nonot nocons
 
*** Column 1:
reghdfe lconsultas0_pc $rf_level ${if}, $fe $cluster
	sum consultas0_pc if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(no checks) replace

*** Column 2:	
reghdfe llow_weight_pc $rf_level ${if}, $fe $cluster
	sum low_weight_pc if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(low_weight)

*** Column 3:
reghdfe lcob01_bcg $rf_level ${if}, $fe $cluster
	sum cob01_bcg if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(BCG)

*** Column 4:
reghdfe lcob01_dpt $rf_level ${if}, $fe $cluster
	sum cob01_dpt if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(DPT)

*** Column 5:
reghdfe lcob01_pol $rf_level ${if}, $fe $cluster
	sum cob01_pol if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(Polio)

*** Column 6:
reghdfe ltmi $rf_level ${if}, $fe $cluster
	sum tmi if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(tmi)

*** Column 7:
reghdfe ldefunciones01_pc $rf_level ${if}, $fe $cluster
	sum defunciones01_pc if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(tmi_own)

*** Column 8:
reghdfe leda_ira01_pc $rf_level ${if}, $fe $cluster
	sum eda_ira01_pc if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(tmi eda ira) 


** Panel B:
local out outreg2 using "${dpmain}\03-table3_b", dec(3)  tex(frag) nonot nocons nor2
 
*** Column 1: 
ivreghdfe lconsultas0_pc $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(no checks) $add_iv replace	

*** Column 2:
ivreghdfe llow_weight_pc $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(low_weight) $add_iv 

*** Column 3:
ivreghdfe lcob01_bcg $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(BCG) $add_iv 

*** Column 4:
ivreghdfe lcob01_dpt $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(DPT) $add_iv 

*** Column 5:
ivreghdfe lcob01_pol $iv_level ${if}, $fe $cluster ffirst
$F_tax
$F_roy
$test_level
$p_val
`out' ctitle(Polio) $add_iv 

*** Column 6:
ivreghdfe ltmi $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(tmi) $add_iv 

*** Column 7:
ivreghdfe ldefunciones01_pc $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(tmi_alt) $add_iv 

*** Column 8:
ivreghdfe leda_ira01_pc $iv_level ${if}, $fe $cluster ffirst
	$F_tax
	$F_roy
	$test_level
	$p_val
	`out' ctitle(tmi eda ira) $add_iv 

* Table 4: Corruption ----------------------------------------------------------

use "${dpwork}\ESTIMATIONS-TERM.dta", clear

** Panel A:
local out outreg2 using "${dpmain}\04-table4_a", dec(3)  tex(frag) nonot nocons

*** Column 1:
reghdfe alcalde $rf_period ${if_period}, $fe_period $cluster_period 	
	sum `e(depvar)' if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(D_mayor) replace	

*** Column 2:
reghdfe alcalde_sum $rf_period ${if_period}, $fe_period $cluster_period 	
	sum `e(depvar)' if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(sum_mayor) 

*** Column 3:
reghdfe alcalde_1 $rf_period ${if_period}, $fe_period $cluster_period 	
	sum `e(depvar)' if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(D_mayor_spend) 	

*** Column 4:
reghdfe alcalde_2 $rf_period ${if_period}, $fe_period $cluster_period 	
	sum `e(depvar)' if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(D_mayor_pol) 	

*** Column 5:
reghdfe alcalde_3 $rf_period ${if_period}, $fe_period $cluster_period 	
	sum `e(depvar)' if e(sample)
	`out' adds(DV mean,r(mean)) ctitle(D_mayor_other)  	

		
** Panel B:
local out outreg2 using "${dpmain}\04-table4_b", dec(3) tex(frag) nonot nocons nor2

*** Column 1:
ivreghdfe alcalde $iv_period ${if_period}, $fe_period $cluster_period ffirst 	
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(D_mayor) $add_iv replace

*** Column 2:
ivreghdfe alcalde_sum $iv_period ${if_period}, $fe_period $cluster_period ffirst	
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(sum_mayor) $add_iv  

*** Column 3:
ivreghdfe alcalde_1 $iv_period ${if_period}, $fe_period $cluster_period ffirst 	
$F_tax
$F_roy
$test_log
$p_val
`out' ctitle(D_mayor_spend) $add_iv  

*** Column 4:
ivreghdfe alcalde_2 $iv_period ${if_period}, $fe_period $cluster_period ffirst 	
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(D_mayor_pol) $add_iv  

*** Column 5:
ivreghdfe alcalde_3 $iv_period ${if_period}, $fe_period $cluster_period ffirst 	
	$F_tax
	$F_roy
	$test_log
	$p_val
	`out' ctitle(D_mayor_other) $add_iv    

*-------------------------------------------------------------------------------*
**# B) FIGURES 
*-------------------------------------------------------------------------------*

* Figures 1: Research Design ---------------------------------------------------

use "${dpwork}\ESTIMATIONS.dta", clear

** Panel A:

*** Estimates:
reghdfe y_tax_pred_pcr b4p b3 b2 a1 a2 a3 a4p ${if}, $fe $cluster

*** Save results
tempfile results
parmest,saving(`results', replace)		

*** Figure	
preserve
 use `results', clear			
 
 gen id=_n
 
 replace id=id+1 if _n>=4
 
 set obs 9
 
 **** Base Year:
 mvencode estimate min95 max95,mv(0)
	replace id=4 if id==.
	lab define lb 2 "-3" 3 "-2" 4 "-1" 5 "0" 6 "+1" 7 "+2" 8 "+3" 
	label values id lb
 
 #d ;
 twoway (scatter estimate id, color(black)) 
		(rcap min95 max95 id, color(black)) if id>1&id<9, 
		  ytitle(Property tax revenue per capita)
		  ylabel(, angle(horizontal)) 
		  xtitle(Years since cadastral update) xlabel(2(1)8, valuelabel) 
		  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
		  plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
		  legend(order(1 "Point estimate" 2 "95% CI") rows(1)) 
		  name(event_pred,replace);
 #d cr		  
 graph export "${dpmain}\05-figure1_a.pdf", as(pdf) name("event_pred") replace
restore

** Panel B:

*** Estimates:
reghdfe y_k_rega_pcr Droy_2006 Droy_2007 Droy_2008 Droy_2009 Droy_2010 			///
		Droy_2011 ${if}, $fe $cluster
	
*** Save results:	
tempfile results
parmest,saving(`results', replace)	

*** Figure:
preserve			
 use `results', clear	
 
 replace parm = "Droy_2005" in 7
 
 gen year=substr(parm,6,4)
 
 destring year,replace
 
 replace year=2005 if year==.
 
 sort year
 
 **** Oil Price Index: 
 gen ind_price_oil_cop_r=1.190233
 replace ind_price=1.394467 if year==2006
 replace ind_price=1.285121 if year==2007
 replace ind_price=1.542736 if year==2008
 replace ind_price=1.054039 if year==2009
 replace ind_price=1.152482 if year==2010
 replace ind_price=1.422329 if year==2011
 
 mvencode estimate min95 max95,mv(0)

 #d ;
 twoway (scatter estimate year, color(black)) 
		(rcap min95 max95 year, color(black)) 
		(connect ind_price_oil_cop_r year, 
				msymbol(X) 
				msize(vlarge) 
				yaxis(2)
				color(gs8)
		), 
		  ytitle("Royalties per capita") 
		  ylabel(, angle(horizontal)) 
		  ytitle(Oil price index (2004=1), axis(2)) 
		  ylabel(, angle(horizontal) axis(2)) 
		  xlabel(2005(1)2011) 
		  legend(order(1 "Point estimate" 2 "95% CI" 3 "Oil Price") rows(1))  
		  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
		  name(oil_rega0511,replace);
 #d cr	 
 
 graph export "${dpmain}\05-figure1_b.pdf", as(pdf) name("oil_rega0511") replace
 
restore

cap graph drop *

